<template>
	<view class="fui-wrap">
		<fui-result v-if="error" type="fail" title="操作失败" :descr="error">	
			<fui-button @click="$reLaunch(is_merchant?'merchant/index':'index/index')" width="400rpx" height="84rpx" text="返回主页" type="success" bold :margin="['48rpx','0','24rpx']"></fui-button>
		</fui-result>
		<fui-result v-if="success" type="success" title="操作成功" :descr="success">
			<fui-button @click="$navigateBack()" width="400rpx" height="84rpx" text="返回" type="success" bold :margin="['48rpx','0','24rpx']"></fui-button>
		</fui-result>
		<block v-if="!error && !success">
			<fui-form labelColor="#111111" ref="form" top="0" :model="formData" :show="false">
				<fui-form-item v-if="platform=='PC'" label="车牌号" asterisk prop="plate_number">
					<fui-input placeholder="请输入车牌号" :borderBottom="false" :padding="[0]" v-model="formData.plate_number"></fui-input>
				</fui-form-item>
				<fui-form-item v-else label="车牌号" asterisk arrow prop="plate_number">
					<fui-input readonly @click="$navigateTo('parking/plate?type=input&plate_number='+formData.plate_number)" :borderBottom="false" :padding="[0]" placeholder="请输入车牌号" v-model="formData.plate_number"></fui-input>
				</fui-form-item>
				<fui-form-item label="车主姓名" asterisk prop="contact">
					<fui-input placeholder="请输入车主姓名" :borderBottom="false" :padding="[0]" v-model="formData.contact"></fui-input>
				</fui-form-item>
				<fui-form-item label="手机号" asterisk prop="mobile">
					<fui-input placeholder="请输入手机号" :borderBottom="false" :padding="[0]" v-model="formData.mobile"></fui-input>
				</fui-form-item>
			</fui-form>
			<view class="fui-bottom__box">
				<fui-button :disabled="disabledButton" text="提交" bold @click="confirmPlate"></fui-button>
			</view>
			<pcfooter></pcfooter>
		</block>
	</view>
</template>

<script>
	import {methods,baseUrl} from '@/utils/core.js';
	import pcfooter from '@/components/common/pcfooter.vue';
	export default {
		components:{
			pcfooter:pcfooter
		},
		data() {
			return {
				error:'',
				success:'',
				platform:'',
				disabledButton:false,
				parking:'',
				safebottom:0,
				formData:{
					qrcode_id:'',
					plate_number:'',
					contact:'',
					mobile:''
				}
			}
		},
		onLoad(e) {
			this.formData.qrcode_id=e.qrcode_id;
			this.$platform();
			this.init();
			let that=this;
			uni.getSystemInfo({
			  success: function (info) {
				that.safebottom=info.safeAreaInsets.bottom*2;
			  }
			});
		},
		onShow() {
			let plate_number=uni.getStorageSync('plate_number') || '';
			if(plate_number){
				this.formData.plate_number=plate_number;
				uni.setStorageSync('plate_number','');
			}
		},
		methods: {
			...methods,
			init:function(){
				this.$get('merchant/car/write').then(res=>{
					this.parking=res.parking;
				}).catch(e=>{
					this.error=e.msg;
				});
			},
			cancelPlate:function(){
				this.$navigateBack();
			},
			confirmPlate:function(){
				if(!this.formData.plate_number){
					this.$msg('请输入车牌号');
					return;
				}
				if(!this.formData.contact || this.formData.contact.length>10){
					this.$msg('请输入准确的车主姓名');
					return;
				}
				let pattern = /^1[3-9]\d{9}$/; 
				if(!pattern.test(this.formData.mobile)){
					this.$msg('请输入正确车主手机号');
					return;
				}
				this.disabledButton=true;
				this.$post('merchant/car/confirm',this.formData,true,true).then(res=>{
					this.success='成功添加【'+this.formData.plate_number+'】为预约车';
				}).catch(e=>{
					this.disabledButton=false;
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.fui-list__item{
		display: flex;
		justify-content: space-between;
		width: 50%;
	}
	.fui-bottom__box{
		margin: 30rpx auto;
		width: 98%;
	}
</style>